package cn.bytest.netty.code;

import cn.bytets.rpc.api.code.ChannelByteBuf;
import io.netty.buffer.ByteBuf;

public class NettyChannelByteBuf implements ChannelByteBuf {

    private final ByteBuf buffer;

    public NettyChannelByteBuf(ByteBuf buffer){
        this.buffer = buffer;
    }
    @Override
    public int writerIndex() {
        return buffer.writerIndex();
    }

    @Override
    public void writeIndex(int index) {
        this.buffer.writerIndex(index);
    }

    @Override
    public void writeBytes(byte[] src) {
        this.buffer.writeBytes(src);
    }

    @Override
    public void writeBytes(byte[] src, int index, int length) {
        this.buffer.writeBytes(src,index,length);
    }

    @Override
    public void writeByte(int value) {
        this.buffer.writeByte(value);
    }

    @Override
    public void writeInt(int value) {
        this.buffer.writeInt(value);
    }

    @Override
    public void markReaderIndex() {
        this.buffer.markReaderIndex();
    }

    @Override
    public int readerIndex() {
        return this.buffer.readerIndex();
    }

    @Override
    public void readBytes(byte[] dist, int start, int len) {
        this.buffer.readBytes(dist,start,len);
    }

    @Override
    public int capacity() {
        return this.buffer.capacity();
    }

    @Override
    public void readerIndex(int index) {
        this.buffer.readerIndex(index);
    }

    @Override
    public void resetReaderIndex() {
        this.buffer.resetReaderIndex();
    }
    @Override
    public int compareTo(ChannelByteBuf o) {
        return 0;
    }

    /**
     * Returns the number of readable bytes which is equal to {@code
     * (this.writerIndex - this.readerIndex)}.
     */
    @Override
    public int readableBytes() {
        return this.writerIndex()-this.readerIndex();
    }

    @Override
    public void readBytes(byte[] src) {
        this.buffer.readBytes(src);
    }
}
